package com.example.element.module.lifecycle.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.base.AbsActivity;
import com.example.common.utils.L;
import com.example.element.R;
import com.example.element.Utils.RouterControl;
import com.example.element.databinding.ActLifecycleHomeBinding;

/**
 * 1.正常启动：onCreate、onResume、onStart
 * 2.A 启动 B：A:onPause、B:onCreate、onStart、onResume、A:onStop
 * 3.B 返回 A：B:onPause、A:onRestart、onStart、onResume、B：onStop、onDestroy;如果长时间A被杀死，会调用onCreate
 * 4.A 启动 B,B没有完全遮住A:A:onPause、B:onCreate、onStart、onResume
 * 点击返回：B:onPause、A:onResume、B:onStop、onDestroy
 * 点击home：B:onPause、A:onStop、B:onStop
 * 切换前台：A:onRestart、onStart、B:onRestart、onStart、onResume
 * 启动dialog：什么都不会发生 【dialog依附于 activity, 并没有被遮挡】
 * A启动dialog，dialog启动A：old A:onnPause、new A:onCreate、onStart、onResume、old A：onStop
 */
@Route(path = "/lifecycle/home")
public class HomeActivity extends AbsActivity<ActLifecycleHomeBinding> {

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        L.e(TAG, "onSaveInstanceState");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    /**
     *
     * @param newConfig
     * local 设备的本地位置发生了改变，一般指切换了系统语言。
     * keyboardHidden 键盘的可访问性发生了改变，比如用户调出了键盘。
     * orientation 屏幕方向发生了改变，比如旋转了手机屏幕。
     */
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.e(TAG, "onCreate");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        L.e(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        L.e(TAG, "onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        L.e(TAG, "onStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        L.e(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        L.e(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        L.e(TAG, "onDestroy");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_lifecycle_home;
    }

    @Override
    protected void main() {

    }

    @Override
    protected void initListener() {
        mBinding.btnFragLife.setOnClickListener(v -> {
            RouterControl.jumpFragLifecycle();
        });

        mBinding.btnNewActivity.setOnClickListener(v -> {
            NewActivity.open(this);
        });

        mBinding.btnAlphaActivity.setOnClickListener(v -> {
            AlphaActivity.open(this);
        });

        mBinding.btnDialog.setOnClickListener(v -> {
            showDialog();
        });

        mBinding.btnAlertDialog.setOnClickListener(v -> {
            showAlearDialog();
        });
    }

    private void showDialog() {
        Dialog dialog = new Dialog(mContext);
        dialog.setTitle("dialog");
        dialog.setContentView(R.layout.act_splash);
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.show();
    }

    private void showAlearDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("show alert dialog");
        builder.setPositiveButton("Ok", null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
